#!/bin/python
#-*- coding:utf-8 -*-
# Filename:    main.py
# Revision:    1.0
# Date:        2012-06-14
# Author:      simonzhang
# web:         www.simonzhang.net
# Email:       simon-zzm@163.com
### END INIT INFO
import sys
import hashlib
import tornado.ioloop
import tornado.locale
import tornado.web
from dbmodel import *
from interface import *


class EditUserHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        _get_user = self.check_user()
        _domain, _title = if_get_domain_title()
        _get_user_left_menu = if_get_user_left_menu(_get_user)
        _get_now_user_passwd_md5 = db_get_edit_user_passwd_md5(_get_user)
        _get_old_passwd = self.get_argument('textfield2')
        _get_old_passwd_md5 = hashlib.md5("%s+%s" % (_get_user, _get_old_passwd)).hexdigest()
        try:
            _get_new_passwd = self.get_argument('textfield3')
            _get_double_new_passwd = self.get_argument('textfield4')
        except:
            _get_new_passwd = 'haha'
            _get_double_new_passwd = 'gege'
        try :
            _get_email = self.get_argument('textfield5')
        except:
            _get_email = 'null@null'
        if _get_new_passwd == _get_double_new_passwd:
            if _get_now_user_passwd_md5 == _get_old_passwd_md5:
                _new_passwd_md5 = hashlib.md5("%s+%s" % (_get_user, _get_new_passwd)).hexdigest()
                db_edit_user_passwd(_get_user, _new_passwd_md5, _get_email)
                self.clear_all_cookies()
                self.redirect("/")
            else:
                self.write("原始密码错误")
        else:
            self.write("两次输入的密码不同")


class InfoHelpHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        _get_user = self.check_user()
        _domain, _title = if_get_domain_title()
        _get_user_left_menu = if_get_user_left_menu(_get_user)
        self.render("templates/info_help.html", domain=_domain, title=_title, \
                                               username = _get_user, \
                                               items=_get_user_left_menu, _user_name = _get_user)
    def post(self):
        self.clear_all_cookies()
        self.redirect("/")

